Skip to content

Method: CddbResponse(Optional, String)

1: /*
2: * *********************************************************************************************************************
3: *
4: * blueMarine II: Semantic Media Centre
5: * http://tidalwave.it/projects/bluemarine2
6: *
7: * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/bluemarine2-src
23: * git clone https://github.com/tidalwave-it/bluemarine2-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.bluemarine2.metadata.cddb.impl;
28:
29: import javax.annotation.Nonnull;
30: import java.util.Optional;
31: import java.util.function.Function;
32: import org.springframework.http.HttpStatus;
33: import org.springframework.http.ResponseEntity;
34: import it.tidalwave.bluemarine2.rest.RestException;
35: import it.tidalwave.bluemarine2.rest.RestResponse;
36: import lombok.extern.slf4j.Slf4j;
37:
38: /***********************************************************************************************************************
39: *
40: * This class encapsulates a response from the CDDB API, including the requested datum - if available - and some
41: * status codes.
42: *
43: * @author Fabrizio Giudici
44: *
45: **********************************************************************************************************************/
46: @Slf4j
47: class CddbResponse<T> extends RestResponse<T>
48: {
49: /*******************************************************************************************************************
50: *
51: *
52: ******************************************************************************************************************/
53: public CddbResponse (@Nonnull final Optional<T> datum, @Nonnull final String responseStatus)
54: {
55: super(datum, responseStatus);
56: }
57:
58: /*******************************************************************************************************************
59: *
60: * Creates a {@code Response} containing a datum out of a {@link ResponseEntity} applying a parser. The parser
61: * receives an XML DOM as the input - it typically uses XPath to extract information.
62: *
63: * @param <X> the type of the datum
64: * @param response the HTTP response
65: * @param parser the parser that produces the datum
66: * @return the {@code Response}
67: *
68: ******************************************************************************************************************/
69: @Nonnull
70: public static <X> CddbResponse<X> of (@Nonnull final ResponseEntity<String> response,
71: @Nonnull final Function<String, X> parser)
72: {
73: final int httpStatus = response.getStatusCodeValue();
74:
75: if (httpStatus != HttpStatus.OK.value())
76: {
77: throw new RestException("Unexpected HTTP status", response.getStatusCode());
78: }
79:
80: final String responseStatus = response.getBody().split("\n")[0].split(",")[0].split(" ")[0];
81: log.debug(">>>> responseStatus: {}", responseStatus);
82:
83: switch (responseStatus)
84: {
85: case "401":
86: return new CddbResponse<>(Optional.empty(), responseStatus);
87:
88: case "210":
89: return new CddbResponse<>(Optional.of(parser.apply(response.getBody())), responseStatus);
90:
91: default:
92: throw new RestException("Unexpected response status", responseStatus);
93: }
94: }
95: }